package com.navinfo.android.qcniscan.scan;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.navinfo.android.qcniscan.scan.view.MyHelpDialog;
import com.navinfo.android.qcniscan.util.FakeR;
import com.navinfo.android.qcniscan.zxing.activity.CaptureActivity;
import com.navinfo.android.qcniscan.zxing.camera.CameraManager;


public class HomeCaptureActivity extends CaptureActivity implements View.OnClickListener,
        OnFailInterface.Capture {

    private static final String TAG = HomeCaptureActivity.class.getSimpleName();
    private int ifOpenLight = 0; //判断是否开启闪光灯
    private ImageView ivLight;
    private TextView tvLight;
    private LinearLayout llInput;
    private LinearLayout llLight;
    private MyHelpDialog myDialog;
    private static FakeR fakeR;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 向 ScanBaseActivity 传递数据
        captureFail = this;
    }

    public void initView() {
        fakeR = new FakeR(this);
        setContentView(fakeR.getId("layout", "activity_page_capture"));
        llLight = (LinearLayout) findViewById(fakeR.getId("id", "ll_light"));
        llInput = (LinearLayout) findViewById(fakeR.getId("id", "ll_input"));
        ivLight = (ImageView) findViewById(fakeR.getId("id", "iv_light"));
        tvLight = (TextView) findViewById(fakeR.getId("id", "tv_light"));
        progressBar = (ProgressBar) findViewById(fakeR.getId("id", "progressBar"));
        ImageView iv_arrow = (ImageView) findViewById(fakeR.getId("id", "iv_arrow"));
        ImageView iv_help = (ImageView) findViewById(fakeR.getId("id", "iv_help"));
        llInput.setOnClickListener(this);
        llLight.setOnClickListener(this);
        iv_arrow.setOnClickListener(this);
        iv_help.setOnClickListener(this);
        myDialog = new MyHelpDialog(this, fakeR.getId("style", "dialogStyle"), MyHelpDialog.HELP_TYPE_HOME);
        myDialog.setTip("扫码目标为驾驶室挡风玻璃左上角二维码", "扫码目标为车辆前面罩右上角的二维码");
        myDialog.setCancelable(false);
        myDialog.setCanceledOnTouchOutside(false);
        myDialog.show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                myDialog.dismiss();
            }
        }, 3000);
    }

    // 是否开启闪光灯
    public void IfOpenLight(View v) {
        ifOpenLight++;
        switch (ifOpenLight % 2) {
            case 0:
                //关闪光灯
                if (CameraManager.get().closeLight()) {
                    ivLight.setImageResource(fakeR.getId("drawable", "icon_light_close"));
                    tvLight.setText("打开手电筒");
                }
                break;
            case 1:
                //开闪光灯
                if (CameraManager.get().openLight()) {
                    ivLight.setImageResource(fakeR.getId("drawable", "icon_light_open"));
                    tvLight.setText("关闭手电筒");
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == fakeR.getId( "id", "ll_input") ){
            input_vin();
        }else if (v.getId() == fakeR.getId( "id", "ll_light")) {
            IfOpenLight(v);
        }else if (v.getId() == fakeR.getId( "id", "iv_arrow")){
            arrowReturn();
        }else if (v.getId() == fakeR.getId( "id", "iv_help")){
            myDialog.show();
        }
    }

    //点击返回按钮
    public void arrowReturn(){
        Intent intent = new Intent();
        intent.putExtra("CANCEl_RESULT", "arrow");
        setResult(RESULT_CANCELED, intent);
        finish();
    }

    //手工输入控制
    public void input_vin() {
        Intent intent = new Intent();
        intent.putExtra("CANCEl_RESULT", "input");
        setResult(RESULT_CANCELED, intent);
        finish();
    }

    // 处理扫码数据
    public void dealData(String data, String format) {
        Intent intent = new Intent();
        intent.putExtra("SCAN_RESULT", data);
        intent.putExtra("SCAN_RESULT_FORMAT", format);
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    public void onFail(int resultCode, String message) {
//        Intent intent = new Intent();
//        intent.putExtra("SCAN_RESULT", data);
//        intent.putExtra("SCAN_RESULT_FORMAT", format);
//        setResult(RESULT_OK, intent);
        finish();
    }


    public void setStatus() {
        initCapture();
    }
}